/*
 * Copyright 2011-2012 by The Regents of the University of California Licensed
 * under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. you may obtain a copy of the
 * License from
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package edu.ucsd.db.bassarisk.util;

import java.util.Arrays;

/**
 * Quick and dirty array of boolean flags. Supports a small set of features
 * found useful in parts of the optimizer but not necessarily supported by other
 * similar classes (e.g {@link java.util.BitSet}).
 */
public class BooleanFlags {
  private final boolean[] flags;

  public BooleanFlags(final int size, final boolean defaultVal) {
    this.flags = new boolean[size];
    Arrays.fill(this.flags, defaultVal);
  }

  public boolean allFalse() {
    for (boolean val : this.flags) {
      if (val) { return false; }
    }
    return true;
  }

  public boolean allTrue() {
    for (boolean val : this.flags) {
      if (!val) { return false; }
    }
    return true;
  }

  public void makeFalse(final int i) {
    this.flags[i] = false;
  }

  public void makeTrue(final int i) {
    this.flags[i] = true;
  }

  public BooleanFlags xor(final BooleanFlags other) {
    if (this.flags.length != other.flags.length) {
      String msg = "Operation requires two flag vectors of the same length.";
      throw new RuntimeException(msg);
    }
    BooleanFlags xor = new BooleanFlags(this.flags.length, false);
    for (int i = 0; i < this.flags.length; ++i) {
      xor.flags[i] = this.flags[i] != other.flags[i];
    }
    return xor;
  }
}